package API.Class.File.AutoOpenSoftware;


import java.io.*;

class Reader {
    private File f = null;
    private final File Cache = new File("data/RunSoftPath.json");

    public void CleanData() {
        try {
            // 创建RandomAccessFile对象，以读写模式打开文件
            RandomAccessFile file = new RandomAccessFile(Cache, "rw");
            // 将文件指针移动到文件开头
            file.seek(0);
            // 清除文件中的数据
            file.setLength(0);
            // 关闭文件
            file.close();
        } catch (IOException e) {

        }
    }

    public Reader(File f) throws IOException {
        this.f = f;
        if (!new File("data").exists()) {
            new File("data").mkdir();
        }
        if (!Cache.exists()) {
            Cache.createNewFile();
        }
    }

    public String[] Read() throws IOException {
        FileInputStream f1 = null;
        BufferedInputStream bu = null;
        String[] result = null;
        f1 = new FileInputStream(Cache);
        bu = new BufferedInputStream(f1);
        byte[] b = new byte[102400];//缓冲区
        int index = bu.read(b);
        String str = new String(b, 0, index);
        result = str.split("\\|");

        if (result != null) {
            for (int i = 0; i < result.length; i++) {
                result[i] = result[i].trim();
            }
        }
        return result;
    }

    public void Write(String Path) throws IOException {
        FileOutputStream f1 = null;
        BufferedOutputStream bu = null;
        f1 = new FileOutputStream(Cache, true);
        bu = new BufferedOutputStream(f1);
        byte[] b = (Path + "|\n").getBytes();
        bu.write(b);
        bu.flush();
    }
}
